package com.maxus.mini.backend.open.api.controller.ecard;

import java.util.List;

import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.dubbo.config.annotation.Reference;
import com.maux.mini.data.api.redis.RedisService;
import com.maux.mini.data.api.user.UserService;
import com.maxus.mini.backend.open.api.controller.BaseController;
import com.maxus.mini.backend.open.api.vo.req.P100006Vo;
import com.maxus.mini.backend.open.api.vo.req.P100007Vo;
import com.maxus.mini.backend.open.api.vo.req.P100008Vo;
import com.maxus.mini.backend.open.api.vo.req.P800005Vo;
import com.maxus.mini.backend.open.api.vo.res.PR100006Vo;
import com.maxus.mini.backend.open.api.vo.res.PR100007Vo;
import com.maxus.mini.backend.open.api.vo.res.PR100008Vo;
import com.maxus.mini.backend.open.api.vo.res.PR800005Vo;
import com.maxus.mini.common.ElumClass.EntityState;
import com.maxus.mini.common.PageImplE;
import com.maxus.mini.common.utils.BeanUtilsExtended;
import com.maxus.mini.common.vo.ResBody;
import com.maxus.mini.ecard.data.api.ecard.ECardService;
import com.maxus.mini.ecard.entity.ecard.ECard;
import com.maxus.mini.ecard.entity.vo.ecard.ECardVo;
import com.maxus.mini.entity.user.User;

/**
 * 用户controller
 * 
 * @author wujinsong
 *
 */
@RestController()
@RequestMapping(value = "/api/v1.0/ecard")
public class ECardController extends BaseController {
	@Reference(version = "1.0.0")
	private ECardService eCardService;
	@Reference(version = "1.0.0")
	private RedisService redisService;
	@Reference(version = "1.0.0")
	public UserService<User, User> userService;

	/**
	 * 列表
	 * 
	 * @param req
	 * @return
	 */
	@RequestMapping(value = "list", method = { RequestMethod.POST })
	@ResponseBody()
	public ResBody list(@RequestBody P800005Vo req) {
		PR800005Vo retVo = new PR800005Vo();
		List<ECard> list = null;

		ECardVo vo = new ECardVo();
		try {
			BeanUtilsExtended.copyProperties(vo, req);
			PageImplE<ECard> pageImplE = eCardService.page(vo);
			list = pageImplE.getContent();
			retVo.setTotalElements(pageImplE.getTotalElements());
		} catch (Exception e) {
			System.out.println(e.getMessage());
		}

		retVo.setItemVos(list);

		return retVo;
	}

	/**
	 * 新增模板
	 * 
	 * @param req
	 * @return
	 */
	@RequestMapping(value = "add")
	@ResponseBody()
	public ResBody add(@RequestBody P100006Vo req) {
		PR100006Vo retVo = new PR100006Vo();
		// User user = this.getCurrent(req);
		ECard eCard = new ECard();
		BeanUtilsExtended.copyProperties(eCard, req);
		eCard.setTemplate(true);
		// eCard.setUser(user);

		eCardService.save(eCard);

		return retVo;
	}

	/**
	 * 新增模板
	 * 
	 * @param req
	 * @return
	 */
	@RequestMapping(value = "edit")
	@ResponseBody()
	public ResBody edit(@RequestBody P100007Vo req) {
		PR100007Vo retVo = new PR100007Vo();
		ECard eCard = eCardService.find(req.getId());
		BeanUtilsExtended.copyProperties(eCard, req);
		eCard.setTemplate(true);

		eCardService.save(eCard);

		return retVo;
	}

	/**
	 * 删除电子贺卡
	 * 
	 * @param req
	 * @return
	 */
	@RequestMapping(value = "delete")
	@ResponseBody()
	public ResBody delete(@RequestBody P100008Vo req) {
		PR100008Vo retVo = new PR100008Vo();
		ECard eCard = eCardService.find(req.getId());
		eCard.setEntityState(EntityState.D.name());

		eCardService.save(eCard);

		return retVo;
	}
}
